package com.ohxwj.modules.web.controller;

import com.ohxwj.common.annotation.SysLog;
import com.ohxwj.common.utils.R;
import com.ohxwj.modules.web.entity.WebLinkEntity;
import com.ohxwj.modules.web.service.WebLinkService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2018-04-16.
 */
@RestController
@RequestMapping("/web/link")
public class WebLinkController extends AbstractAction {

    @Autowired
    private WebLinkService webLinkService;

    @RequestMapping("/list")
    public R list(HttpServletRequest request) {
        int type = getIntegerParamFromRequest(request, "type");
        List<WebLinkEntity> webBannerEntities = webLinkService.queryLinks(type);
        return R.ok().put("data", webBannerEntities);
    }

//    @RequestMapping("/save")
//    public R save(HttpServletRequest request) {
//        WebLinkEntity linkEntity = new WebLinkEntity();
//        String url = getStringParamFromRequest(request, "url");
//        String title = getStringParamFromRequest(request, "title");
//        String icon = getStringParamFromRequest(request, "icon");
//        int type = getIntegerParamFromRequest(request, "type");
//        linkEntity.setIcon(icon);
//        linkEntity.setTitle(title);
//        linkEntity.setUrl(url);
//        linkEntity.setType(type);
//        webLinkService.insertWebLinks(linkEntity);
//        return R.ok();
//    }

    @SysLog("add link")
    @RequestMapping("/save")
    @RequiresPermissions("web:link:save")
    public R save(@RequestBody WebLinkEntity linkEntity) {

        webLinkService.insertWebLinks(linkEntity);
        return R.ok();
    }

    @SysLog("update link")
    @RequestMapping("/update")
    @RequiresPermissions("web:link:update")
    public R update(@RequestBody WebLinkEntity linkEntity) {
        webLinkService.updateWebLinks(linkEntity);
        return R.ok();
    }

    @SysLog("delete link")
    @RequestMapping("/delete")
    @RequiresPermissions("web:link:delete")
    public R delete(int id) {
//        int id = getIntegerParamFromRequest(request, "id");
        webLinkService.deleteWebLinks(id);
        return R.ok();
    }
}
